﻿using HalconDotNet;
using MaterialDesignDemo.HalconCS;
using Microsoft.Win32;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

namespace MaterialDesignDemo.UserControls
{
    /// <summary>
    /// HalconPage.xaml 的交互逻辑
    /// </summary>
    public partial class HalconPage : UserControl
    {
        public HalconPage()
        {
            InitializeComponent();
        }

        HImage image = new HImage();
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.ShowDialog();
                //string fileName = "C:\\Users\\Public\\Documents\\MVTec\\HALCON-18.11-Progress\\examples\\images\\printer_chip\\printer_chip_01.png";

                image = new HImage(ofd.FileName);
                int width, height;
                image.GetImageSize(out width, out height);
                hSmart.HalconWindow.SetPart(0, 0, height, width);

                hSmart.HalconWindow.DispObj(image);
                //BitmapImage bitmapImage = new BitmapImage();
                //bitmapImage.BeginInit();
                //bitmapImage.UriSource = new Uri(fileName, UriKind.RelativeOrAbsolute);
                //bitmapImage.EndInit();
                //hSmart.Width = bitmapImage.Width;
                //hSmart.Height = bitmapImage.Height;
                //hSmart.Source = bitmapImage;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           
        }
        List<HDrawingobjectModel> hDrawingObjects = new List<HDrawingobjectModel>();
        private void CreateCircle_Click(object sender, RoutedEventArgs e)
        {
            HTuple[] hTuples = new HTuple[] { 100, 100, 50 };
            var drawingObject = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE, hTuples);
            drawingObject.OnDrag(HDrawingObjectCallbackClass);
            hDrawingObjects.Add(new HDrawingobjectModel
            {
                hDrawingObject = drawingObject,
                Htuples = hTuples
            });
            hSmart.HalconWindow.AttachDrawingObjectToWindow(drawingObject);
        }
        public void HDrawingObjectCallbackClass(HDrawingObject drawid, HWindow window, string type)
        {
            var htuples = new HTuple[3];
            htuples[0]=drawid.GetDrawingObjectParams("row");
            htuples[1]=drawid.GetDrawingObjectParams("column");
            htuples[2]=drawid.GetDrawingObjectParams("radius");

            var drawobj = hDrawingObjects.FirstOrDefault(s => s.hDrawingObject.ID.Equals(drawid.ID));
            if (drawobj != null)
            {
                drawobj.Htuples = htuples;
            }
            System.Diagnostics.Debug.WriteLine($"id:{drawid?.ID},row:{htuples[0]},column:{htuples[1]},radius:{htuples[2]}");
        }

        private void CreateRectangle_Click(object sender, RoutedEventArgs e)
        {
            //var drawingObject = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE1, new HTuple[] { 100, 100, 250,250 });
            //hDrawingObjects.Add(drawingObject);
            //hSmart.HalconWindow.AttachDrawingObjectToWindow(drawingObject);
        }

        HTuple hv_ModelID;
        private void CreateCircleModel_Click(object sender, RoutedEventArgs e)
        {
            HObject ho_Circle;
            HObject ho_ImageReduce;
            HObject GrayImage;
            hv_ModelID =new HTuple();
            var htuples= hDrawingObjects[0].Htuples;

            //生成圆
            //得到运行区域对应的图像
            //得到这个图像对应的匹配模版
            HOperatorSet.GenCircle(out ho_Circle, htuples[0], htuples[1], htuples[2]);

            HOperatorSet.Rgb1ToGray(image,out GrayImage);
            HOperatorSet.ReduceDomain(GrayImage, ho_Circle,out ho_ImageReduce);

            HOperatorSet.CreateScaledShapeModel(ho_ImageReduce,"auto",-0.39,0.79,"auto",0.9,1.1,"auto","auto","use_polarity","auto","auto",out hv_ModelID);
        }

        private void BtnMatch_Click(object sender, RoutedEventArgs e)
        {
            txtResult.Text = "";
            var result = new TemplateService().Execute(hSmart.HalconWindow, image, hv_ModelID);
            if (result.Count == 0)
                txtResult.Text = "没匹配到";
            else
            {
                foreach (var item in result)
                {
                    txtResult.Text += $"Row:{item.row},column:{item.column},Score:{item.score}\r\n";
                };
            }
        }

    }
    public class HDrawingobjectModel
    { 
        public HTuple[] Htuples { get; set; }
        public HDrawingObject hDrawingObject { get; set; }
    }
}
